﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace LibraryManagement.UserControls
{
    public partial class UCAuthors : System.Web.UI.UserControl
    {
        
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                BindGridAuthor();
        }
        /// <summary>
        /// Handles the RowCommand event of the GrdAuthor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
        protected void GrdAuthor_RowCommand(object sender, GridViewCommandEventArgs e)
        {
          
            ViewState["AuthorID"]= null;
            if (e.CommandName == "EditAuthor")
            {
               ViewState["AuthorID"] = Convert.ToInt32(e.CommandArgument);
               DataTable dtAuthor = DBController.GetAuthorList(Int32.Parse(this.ViewState["AuthorID"].ToString()),null);
               txtAuthorName.Text = dtAuthor.Rows[0]["AuthorName"].ToString();
               ChkActive.Checked = Convert.ToBoolean(dtAuthor.Rows[0]["Active"].ToString());
               BtnSave.Visible = false; BtnUpdate.Visible = true;
            }
            if (e.CommandName == "DeleteAuthor")
            {
                ViewState["AuthorID"] = Convert.ToInt32(e.CommandArgument);
                DBController.DeleteAuthor(Int32.Parse(this.ViewState["AuthorID"].ToString()));
            }
            BindGridAuthor();
        }
        /// <summary>
        /// Handles the Sorting event of the GrdAuthor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewSortEventArgs"/> instance containing the event data.</param>
        protected void GrdAuthor_Sorting(object sender, GridViewSortEventArgs e)
        {
            String sort = e.SortExpression;
            if (((String)Session["sortDirection"] == "ASC") || (Session["sortDirection"] == null))
            {
                sort = e.SortExpression + " DESC";
                Session["sortDirection"] = "DESC";
            }
            else
            {
                sort = e.SortExpression + " ASC";
                Session["sortDirection"] = "ASC";
            }
            DataTable dt = DBController.GetAuthorList(null,null);
            dt.DefaultView.Sort = sort;
            GrdAuthor.DataSource = dt;
            GrdAuthor.DataBind();
        }

        /// <summary>
        /// Handles the PageIndexChanging event of the gvBenefitDescription control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
        protected void GrdAuthor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GrdAuthor.PageIndex = e.NewPageIndex;
            BindGridAuthor();
        }

        /// <summary>
        /// Binds the grid history.
        /// </summary>
        private void BindGridAuthor()
        {
            DataTable dtAuthorList = null;
            dtAuthorList = DBController.GetAuthorList(null,null);
            GrdAuthor.DataSource = dtAuthorList;
            GrdAuthor.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the BtnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = DBController.GetAuthorList(null, txtAuthorName.Text.ToString().Trim());
            if (dt.Rows.Count == 0)
            {
                DBController.InsertUpdateAuthor(null, txtAuthorName.Text.ToString().Trim());
                Reset();
            }
            else
            {
                lblMsg.Text = " This Author already exist.";
            }
            BindGridAuthor();
           
        }

        /// <summary>
        /// Handles the Click event of the BtnUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            DBController.InsertUpdateAuthor(Int32.Parse(this.ViewState["AuthorID"].ToString()), txtAuthorName.Text.ToString().Trim());
            Reset();
            BindGridAuthor();
        }

        /// <summary>
        /// Resets this instance.
        /// </summary>
        private void Reset()
        {
            BtnUpdate.Visible = false;
            BtnSave.Visible = true;
            txtAuthorName.Text = String.Empty;
        }

        /// <summary>
        /// Handles the Click event of the ButtonCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }
    }
}